package linkedlist;

import java.nio.file.NotLinkException;

/**
 * Created by IntelliJ IDEA.
 * User: 杰哥
 * Date: 2020/3/27
 * Time: 0:33
 * 约瑟夫问题
 */
public class Joseph {
    public static void main(String[] args) {
        CircleSingleList list = new CircleSingleList();
        int boys = 5;
        list.addBoy(boys);
//        list.showBoys();
        list.countBoy(1, 2, boys);

    }
}

/**
 * Circle n. 循环，周期；圆；圈子；圆形物
 *Single adj. 单一的；单身的；单程的
 */
class CircleSingleList {
    //创建一个frist节点，当前没有编号
    private Boy first = null;

    // 添加小孩节点，构建成一个环形的链表
    public void addBoy(int nums) {
        if (nums < 1) {
            System.out.println("nums的值不正确");
            return;
        }
        Boy curBoy = null;
        for (int i = 1; i <= nums; i++) {
            Boy boy = new Boy(i);
            if (i == 1) {
                first = boy;
                first.setNext(first);
                curBoy = first;
            } else {
                curBoy.setNext(boy);
                boy.setNext(first);
                curBoy = boy;
            }
        }
    }

    public void showBoys() {
        if (first == null) {
            System.err.println("空");
            return;
        }
        Boy curBoy = first;
        while (true) {
            System.out.printf("小孩的编号%d\n", curBoy.getNo());
            if (curBoy.getNext() == first) {
                break;
            }
            curBoy = curBoy.getNext();
        }

    }


    /**
     * @param startNo  表示从第几个开始
     * @param countNum 表示数几下
     * @param nums     表示最初有多少个小孩在圈中
     */
    public void countBoy(int startNo, int countNum, int nums) {
        if (first == null || startNo < 1 || startNo > nums) {
            System.err.println("输入有误");
        }
        //指向出圈的上一个节点
        Boy helper = first;
        while (true) {
            if (helper.getNext() == first) {
                break;
            }
            helper = helper.getNext();
        }
        //first指向第一个要报数的人
        for(int i = 0; i < startNo - 1; i++) {
            first = first.getNext();
            helper = helper.getNext();
        }

        while (true) {
            if(helper == first) { //圈中只有一个小孩节点
                break;
            }
            for(int j =0; j < countNum - 1; j++) { //开始报数
               first = first.getNext();
               helper = helper.getNext();
            }
            //报完数之后，first指向的就是要出圈的小孩节点
            System.out.printf("编号为%d的小孩出圈\n", first.getNo());
            first = first.getNext(); // 指向下一个开始报数的小孩
            helper.setNext(first);   // 出圈
        }
        System.out.printf("最后留在圈中的小孩编号是%d\n", helper.getNo());
    }


}
//创建一个类，表示一个节点
class Boy {
    private int no;
    private Boy next;

    public Boy(int no) {
        this.no = no;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public Boy getNext() {
        return next;
    }

    public void setNext(Boy next) {
        this.next = next;
    }
}
